package com.fengniu.ejb.dao.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.fengniu.ejb.dao.DeltaDaoLocal;
import com.fengniu.ejb.dao.DeltaDaoRemote;
import com.fengniu.ejb.dao.util.DaoUtil;
import com.fengniu.ejb.dao.util.IEJBConstants;
import com.fengniu.ejb.entity.Category;
import com.fengniu.ejb.entity.Category2Category;
import com.fengniu.ejb.entity.Content;
import com.fengniu.ejb.entity.Content2Category;
import com.fengniu.ejb.entity.Content2Content;
import com.fengniu.ejb.entity.DeltaRelation;
import com.fengniu.ejb.entity.Site;
import com.fengniu.ejb.entity.Site2Category;
import com.fengniu.ejb.entity.Site2Content;
import com.fengniu.ejb.entity.Site2Site;
import com.fengniu.ejb.entity.User;
import com.fengniu.ejb.entity.User2Category;
import com.fengniu.ejb.entity.User2Content;
import com.fengniu.ejb.entity.User2Site;
import com.fengniu.ejb.entity.User2User;

/**
 * @author conan
 * 
 */
@Stateless
@LocalBinding(jndiBinding = IEJBConstants.DeltaDaoLocalJndiName)
@RemoteBinding(jndiBinding = IEJBConstants.DeltaDaoRemoteJndiName)
public class DeltaDaoImpl implements DeltaDaoLocal, DeltaDaoRemote {
	static Logger logger = Logger.getLogger(DeltaDaoImpl.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = 1079908032721853904L;

	@PersistenceContext(unitName = "default")
	protected EntityManager em;

	public DeltaRelation findDeltaRelation(long sysId, String entityName, Integer relationId, long sysId2,
			String entity2Name) {

		List<DeltaRelation> result = this
				.getListDeltaRelation(sysId, entityName, relationId, sysId2, entity2Name, 0, 1);
		if (result != null && result.size() > 0)
			return result.get(0);
		else
			return null;
	}

	@SuppressWarnings("unchecked")
	public List<DeltaRelation> getListSite2SiteContent2(long siteSysId, Integer[] relationIds, long site2ContentSysId,
			Integer[] contentTypeIds, int pgno, int pgsize) {
		Query q = this.getQuerySite2SiteContent2(siteSysId, relationIds, site2ContentSysId, contentTypeIds, null, true);
		List<DeltaRelation> result = DaoUtil.getResultList(q, pgno, pgsize);
		return result;
	}

	public long getCountSite2SiteContent2(long siteSysId, Integer[] relationIds, long site2ContentSysId,
			Integer[] contentTypeIds) {
		Long result = new Long(0);
		Query q = this
				.getQuerySite2SiteContent2(siteSysId, relationIds, site2ContentSysId, contentTypeIds, null, false);

		result = (Long) q.getSingleResult();

		return result;
	}

	@SuppressWarnings("unchecked")
	private Query getQuerySite2SiteContent2(long siteSysId, Integer[] relationIds, long site2ContentSysId,
			Integer[] contentTypeIds, String orderBy, boolean isGetList) {

		String ent = isGetList ? " delta" : "count(delta)";

		String eql = "SELECT " + ent + " from DeltaRelation delta";
		String whereand = "";
		if (siteSysId > 0) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  delta.site.sysId=:siteSysId ";
		}
		whereand = DaoUtil.getWhereOrAndStr(whereand);
		eql += whereand + " delta.site IS NOT NULL";

		if (!DaoUtil.isNull(relationIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  delta.relationId IN (:relationIds) ";
		}

		if (site2ContentSysId > 0) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  delta.site2Content2.sysId=:site2ContentSysId ";
		}
		whereand = DaoUtil.getWhereOrAndStr(whereand);
		eql += whereand + " delta.site2Content2 IS NOT NULL";

		if (!DaoUtil.isNull(contentTypeIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  delta.site2Content2.content.typeId IN (:contentTypeIds) ";
		}

		logger.info("eql=[" + eql + "]");
		Query q = em.createQuery(eql);
		if (siteSysId > 0) {
			q.setParameter("siteSysId", siteSysId);
		}
		if (DaoUtil.isNull(relationIds) == false) {
			List rts = DaoUtil.getListFromArray(relationIds);
			q.setParameter("relationIds", rts);
		}
		if (site2ContentSysId > 0) {
			q.setParameter("site2ContentSysId", site2ContentSysId);
		}
		if (DaoUtil.isNull(contentTypeIds) == false) {
			List rts = DaoUtil.getListFromArray(contentTypeIds);
			q.setParameter("contentTypeIds", rts);
		}

		return q;
	}

	public List<DeltaRelation> getListDeltaRelation(long sysId, String entityName, Integer relationId, long sysId2,
			String entity2Name, int pgno, int pgsize) {
		String eql = "SELECT delta from DeltaRelation delta WHERE ";
		String _AND_ = "";
		if (entityName.equals(User.class.getName())) {
			if (sysId > -1) {
				eql += " delta.user.sysId=:sysId";
				_AND_ = " AND ";
			}
			eql += _AND_ + " delta.user IS NOT NULL";
		} else if (entityName.equals(Content.class.getName())) {
			if (sysId > -1) {
				eql += " delta.content.sysId=:sysId";
				_AND_ = " AND ";
			}
			eql += _AND_ + " delta.content IS NOT NULL";
		} else if (entityName.equals(Site.class.getName())) {
			if (sysId > -1) {
				eql += " delta.site.sysId=:sysId";
				_AND_ = " AND ";
			}
			eql += _AND_ + " delta.site IS NOT NULL";
		} else if (entityName.equals(Category.class.getName())) {
			if (sysId > -1) {
				eql += " delta.category.sysId=:sysId";
				_AND_ = " AND ";
			}
			eql += _AND_ + " delta.category IS NOT NULL";
		} else if (entityName.equals(User2Category.class.getName())) {
			if (sysId > -1) {
				eql += " delta.user2Category.sysId=:sysId";
				_AND_ = " AND ";
			}
			eql += _AND_ + " delta.user2Category IS NOT NULL";
		} else if (entityName.equals(User2Content.class.getName())) {
			if (sysId > -1) {
				eql += " delta.user2Content.sysId=:sysId";
				_AND_ = " AND ";
			}
			eql += _AND_ + " delta.user2Content IS NOT NULL";
		} else if (entityName.equals(User2Site.class.getName())) {
			if (sysId > -1) {
				eql += " delta.user2Site.sysId=:sysId";
				_AND_ = " AND ";
			}
			eql += _AND_ + " delta.user2Site IS NOT NULL";
		} else if (entityName.equals(User2User.class.getName())) {
			if (sysId > -1) {
				eql += " delta.user2User.sysId=:sysId";
				_AND_ = " AND ";
			}
			eql += _AND_ + " delta.user2User IS NOT NULL";
		} else if (entityName.equals(Content2Category.class.getName())) {
			if (sysId > -1) {
				eql += " delta.content2Category.sysId=:sysId";
				_AND_ = " AND ";
			}
			eql += _AND_ + " delta.content2Category IS NOT NULL";
		} else if (entityName.equals(Content2Content.class.getName())) {
			if (sysId > -1) {
				eql += " delta.content2Content.sysId=:sysId";
				_AND_ = " AND ";
			}
			eql += _AND_ + " delta.content2Content IS NOT NULL";
		} else if (entityName.equals(Site2Category.class.getName())) {
			if (sysId > -1) {
				eql += " delta.site2Category.sysId=:sysId";
				_AND_ = " AND ";
			}
			eql += _AND_ + " delta.site2Category IS NOT NULL";
		} else if (entityName.equals(Site2Content.class.getName())) {
			if (sysId > -1) {
				eql += " delta.site2Content.sysId=:sysId";
				_AND_ = " AND ";
			}
			eql += _AND_ + " delta.site2Content IS NOT NULL";
		} else if (entityName.equals(Site2Site.class.getName())) {
			if (sysId > -1) {
				eql += " delta.site2Site.sysId=:sysId";
				_AND_ = " AND ";
			}
			eql += _AND_ + " delta.site2Site IS NOT NULL";
		} else if (entityName.equals(Category2Category.class.getName())) {
			if (sysId > -1) {
				eql += " delta.category2Category.sysId=:sysId";
				_AND_ = " AND ";
			}
			eql += _AND_ + " delta.category2Category IS NOT NULL";
		} else {
			// error parameter
			logger.error("Error parameters");
			return null;
		}

		_AND_ = " AND ";
		if (relationId > -1) {
			eql += _AND_ + " delta.relationId=:relationId";
		}
		if (entity2Name.equals(User.class.getName())) {
			if (sysId2 > -1) {
				eql += _AND_ + " delta.user2.sysId=:sysId2";
			}
			eql += _AND_ + " delta.user2 IS NOT NULL";
		} else if (entity2Name.equals(Content.class.getName())) {
			if (sysId2 > -1) {
				eql += _AND_ + " delta.content2.sysId=:sysId2";
			}
			eql += _AND_ + " delta.content2 IS NOT NULL";
		} else if (entity2Name.equals(Site.class.getName())) {
			if (sysId2 > -1) {
				eql += _AND_ + " delta.site2.sysId=:sysId2";
			}
			eql += _AND_ + " delta.site2 IS NOT NULL";
		} else if (entity2Name.equals(Category.class.getName())) {
			if (sysId2 > -1) {
				eql += _AND_ + " delta.category2.sysId=:sysId2";
			}
			eql += _AND_ + " delta.category2 IS NOT NULL";
		} else if (entity2Name.equals(User2Category.class.getName())) {
			if (sysId2 > -1) {
				eql += _AND_ + " delta.user2Category2.sysId=:sysId2";
			}
			eql += _AND_ + " delta.user2Category2 IS NOT NULL";
		} else if (entity2Name.equals(User2Content.class.getName())) {
			if (sysId2 > -1) {
				eql += _AND_ + " delta.user2Content2.sysId=:sysId2";
			}
			eql += _AND_ + " delta.user2Content2 IS NOT NULL";
		} else if (entity2Name.equals(User2Site.class.getName())) {
			if (sysId2 > -1) {
				eql += _AND_ + " delta.user2Site2.sysId=:sysId2";
			}
			eql += _AND_ + " delta.user2Site2 IS NOT NULL";
		} else if (entity2Name.equals(User2User.class.getName())) {
			if (sysId2 > -1) {
				eql += _AND_ + " delta.user2User2.sysId=:sysId2";
			}
			eql += _AND_ + " delta.user2User2 IS NOT NULL";
		} else if (entity2Name.equals(Content2Category.class.getName())) {
			if (sysId2 > -1) {
				eql += _AND_ + " delta.content2Category2.sysId=:sysId2";
			}
			eql += _AND_ + " delta.content2Category2 IS NOT NULL";
		} else if (entity2Name.equals(Content2Content.class.getName())) {
			if (sysId2 > -1) {
				eql += _AND_ + " delta.content2Content2.sysId=:sysId2";
			}
			eql += _AND_ + " delta.content2Content2 IS NOT NULL";
		} else if (entity2Name.equals(Site2Category.class.getName())) {
			if (sysId2 > -1) {
				eql += _AND_ + " delta.site2Category2.sysId=:sysId2";
			}
			eql += _AND_ + " delta.site2Category2 IS NOT NULL";
		} else if (entity2Name.equals(Site2Content.class.getName())) {
			if (sysId2 > -1) {
				eql += _AND_ + " delta.site2Content2.sysId=:sysId2";
			}
			eql += _AND_ + " delta.site2Content2 IS NOT NULL";
		} else if (entity2Name.equals(Site2Site.class.getName())) {
			if (sysId2 > -1) {
				eql += _AND_ + " delta.site2Site2.sysId=:sysId2";
			}
			eql += _AND_ + " delta.site2Site2 IS NOT NULL";
		} else if (entity2Name.equals(Category2Category.class.getName())) {
			if (sysId2 > -1) {
				eql += _AND_ + " delta.category2Category2.sysId=:sysId2";
			}
			eql += _AND_ + " delta.category2Category2 IS NOT NULL";
		} else {
			// error parameter
			logger.error("Error parameters");
			return null;
		}

		Query q = em.createQuery(eql);

		if (sysId > -1)
			q.setParameter("sysId", sysId);
		if (relationId > -1)
			q.setParameter("relationId", relationId);
		if (sysId2 > -1)
			q.setParameter("sysId2", sysId2);

		List<DeltaRelation> result = DaoUtil.getResultList(q, pgno, pgsize);
		return result;
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public DeltaRelation save(DeltaRelation delta) {
		return em.merge(delta);
	}

	public DeltaRelation findDeltaRelationBy(long deltaSysId) {
		return em.find(DeltaRelation.class, deltaSysId);
	}

}
